package managesystem;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import IO.Hash;
import controlsystem.Course;
import controlsystem.CM;
public class RegisterStudentServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp){
			String un = req.getParameter("username");
			String pw = req.getParameter("password");
			String fname = req.getParameter("firstname");
			String lname = req.getParameter("lastname");
			String email = req.getParameter("email");
			Student s = new Student(fname,lname,email,un,Hash.getHash(pw));
			String[] coursenames = req.getParameterValues("courses");
			System.out.println(coursenames[0]);
			List<Course> availableCourses=CM.get().getAvailableCourses();
			System.out.println(availableCourses.get(0));
			//TODO : courses opslaan bij de student maar eerst klasse student aanpassen
			for(int i = 0; i < coursenames.length;i++){
				for(Course c: availableCourses){
					if(c.getName().equals(coursenames[i])){
						s.addCourse(c);
					}
				}
			}
			SM.get().save(s);
			System.out.println("Student is toegevoegd");
			
			try {
				resp.sendRedirect("index.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	

}
